package com.example.jegotrip.db;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

import com.example.jegotrip.entity.CartInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * 购物车数据库操作
 */
public class CartDBHelper extends SQLiteOpenHelper {
    // 数据库名称
    private static final String DATABASE_NAME = "cart.db";
    // 数据库版本
    private static final int DATABASE_VERSION = 1;
    private static CartDBHelper cartDBHelper;

    public CartDBHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, @Nullable int version) {
        super(context, name, factory, version);
    }

    public static synchronized CartDBHelper getInstance(Context context){
        if (cartDBHelper == null) {
            cartDBHelper = new CartDBHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
        }
        return cartDBHelper;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // 创建users表的SQL语句
        String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS carts (cart_id INTEGER PRIMARY KEY autoincrement default 10," +
                "product_id INTEGER," + //商品id
                "product_img TEXT," + //商品图片
                "product_title TEXT," + //商品名称名
                "product_price INTEGER," + //商品详情
                "product_count INTEGER)"; //商品价格
        db.execSQL(CREATE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // 升级数据库时执行的SQL语句
        db.execSQL("DROP TABLE IF EXISTS carts");
        onCreate(db);
    }

    /**
     * 新增/创建购物车商品信息
     */
    public int createCart(int product_id,
                          String product_img,
                          String product_title,
                          int product_price,
                          int product_count){

        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("product_id", product_id);
        values.put("product_img", product_img);
        values.put("product_title", product_title);
        values.put("product_price", product_price);
        values.put("product_count", product_count);
        String nullColumnHash = "values(null, ?, ?, ?, ?, ?)";
        int insert = (int)db.insert("carts", nullColumnHash, values);
        db.close();
        return insert;
    }

    public List<CartInfo> searchCarts(String product_title){
        SQLiteDatabase db = getWritableDatabase();
        List<CartInfo> cartInfos = new ArrayList<>();
        String sql = "select cart_id,product_id,product_img,product_title,product_price,product_count" +
                "from carts where product_title like?";
        String[] selectedArgs =  new String[]{"%" + product_title + "%"};
        Cursor cursor = db.rawQuery(sql, selectedArgs);
        while (cursor.moveToNext()) {
            @SuppressLint("Range") int cart_id = cursor.getInt(cursor.getColumnIndex("cart_id"));
            @SuppressLint("Range") int product_id = cursor.getInt(cursor.getColumnIndex("product_id"));
            @SuppressLint("Range") String product_img = cursor.getString(cursor.getColumnIndex("product_img"));
            @SuppressLint("Range") String productTitle = cursor.getString(cursor.getColumnIndex("product_title"));
            @SuppressLint("Range") int product_price = cursor.getInt(cursor.getColumnIndex("product_price"));
            @SuppressLint("Range") int product_count = cursor.getInt(cursor.getColumnIndex("product_count"));
            CartInfo cartInfo = new CartInfo(cart_id,product_id,product_img, productTitle, product_price,product_count);
            cartInfos.add(cartInfo);
        }
        cursor.close();
        db.close();
        return cartInfos;
    }

    public List<CartInfo> queryCarts(){
        SQLiteDatabase db = getWritableDatabase();
        List<CartInfo> cartInfos = new ArrayList<>();
        String sql = "select cart_id, product_id,product_img,product_title,product_price,product_count from carts";
        String[] selectedArgs = {};
        Cursor cursor = db.rawQuery(sql, selectedArgs);
        while (cursor.moveToNext()) {
            @SuppressLint("Range") int cart_id = cursor.getInt(cursor.getColumnIndex("cart_id"));
            @SuppressLint("Range") int product_id = cursor.getInt(cursor.getColumnIndex("product_id"));
            @SuppressLint("Range") String product_img = cursor.getString(cursor.getColumnIndex("product_img"));
            @SuppressLint("Range") String product_title = cursor.getString(cursor.getColumnIndex("product_title"));
            @SuppressLint("Range") int product_price = cursor.getInt(cursor.getColumnIndex("product_price"));
            @SuppressLint("Range") int product_count = cursor.getInt(cursor.getColumnIndex("product_count"));
            CartInfo cartInfo=new CartInfo(cart_id,product_id,product_img, product_title,product_price, product_count);
            cartInfos.add(cartInfo);
        }
        cursor.close();
        db.close();
        return cartInfos;
    }

    /**
     * 修改购物车
     * @param cart_id
     * @param cartInfo
     * @return
     */
    public int updateProduct(int cart_id, CartInfo cartInfo){
        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("product_count", cartInfo.getProduct_count() + 1);
        int update = db.update("carts", values, "cart_id=?", new String[]{cart_id + ""});
        db.close();
        return update;
    }

    /**
     * 根据用户名和商品ID判断有没有添加过购物车
     */
    public CartInfo isAddCart(int product_id){
        SQLiteDatabase db = getReadableDatabase();
        CartInfo cartInfo = null;
        String sql = "select cart_id,product_id,product_img,product_title,product_details,product_price,product_count"
                + "from carts where product_id=?";
        String[] selectionArgs = {product_id + ""};
        Cursor cursor = db.rawQuery(sql, selectionArgs);
        if (cursor.moveToNext()){
            @SuppressLint("Range") int cart_id = cursor.getInt(cursor.getColumnIndex("cart_id"));
            @SuppressLint("Range") int productId = cursor.getInt(cursor.getColumnIndex("product_id"));
            @SuppressLint("Range") String productImg = cursor.getString(cursor.getColumnIndex("product_img"));
            @SuppressLint("Range") String productTitle = cursor.getString(cursor.getColumnIndex("product_title"));
            @SuppressLint("Range") int productPrice = cursor.getInt(cursor.getColumnIndex("product_price"));
            @SuppressLint("Range") int productCount = cursor.getInt(cursor.getColumnIndex("product_count"));
            cartInfo = new CartInfo(cart_id,productId,productImg,productTitle,productPrice,productCount);
        }
        cursor.close();
        db.close();
        return cartInfo;
    }
    /**
     * 删除
     */
    public int deleteCart(int product_id){
        SQLiteDatabase db = getWritableDatabase();
        int del = (int)db.delete("carts", "product_id=?", new String[]{product_id + ""});
        db.close();
        return del;
    }
}
